package com.cqie.hospitalescort.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqie.hospitalescort.DTO.AuthUser;
import com.cqie.hospitalescort.DTO.IdDTO;
import com.cqie.hospitalescort.DTO.WelfareShopDTO;
import com.cqie.hospitalescort.entity.EscortInfo;
import com.cqie.hospitalescort.entity.WelfareShop;
import com.cqie.hospitalescort.exception.BusinessException;
import com.cqie.hospitalescort.mapper.EscortInfoMapper;
import com.cqie.hospitalescort.mapper.WelfareShopMapper;
import com.cqie.hospitalescort.service.WelfareShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.hospitalescort.service.file.AliYunService;
import com.cqie.hospitalescort.utils.Asserts;
import com.cqie.hospitalescort.utils.WebSecurityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gdy
 * @since 2025-02-18
 */
@Service
public class WelfareShopServiceImpl extends ServiceImpl<WelfareShopMapper, WelfareShop> implements WelfareShopService {

    @Autowired
    private WelfareShopMapper welfareshopMapper;
    @Autowired
    private AliYunService aliYunService;
    @Autowired
    private EscortInfoMapper escortInfoMapper;

    /**
     * 查看福利商品
     *
     * @param name
     * @return
     */
    @Override
    public List<WelfareShop> findAll(String name) {
        LambdaQueryWrapper<WelfareShop> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(name)) {
            queryWrapper.like(WelfareShop::getName, name);
        }

        return welfareshopMapper.selectList(queryWrapper);
    }

    /**
     * 新增、修改福利商品
     *
     * @param dto
     * @param pictures
     */
    @Override
    @Transactional
    public void addOrEditShop(WelfareShopDTO dto, List<MultipartFile> pictures) {
        try {
            WelfareShop welfareShop = new WelfareShop();
            if (Objects.nonNull(dto.getId())) {
                welfareShop = welfareshopMapper.selectById(dto.getId());
                if (StringUtils.hasText(welfareShop.getPicture())) {
                    aliYunService.deleteFilesByUrl(Arrays.asList(welfareShop.getPicture().split(",")));
                }
            }
            BeanUtils.copyProperties(dto, welfareShop, "id");
            List<String> list = aliYunService.uploadFiles(pictures);
            welfareShop.setPicture(String.join(",", list));
            welfareshopMapper.insert(welfareShop);
        } catch (IOException e) {
            throw new BusinessException("图片上传oss失败");
        }
    }

    /**
     * 兑换商品
     *
     * @param id
     * @param count
     */
    @Override
    @Transactional
    public void exchangeShop(Integer id, Integer count) {
        AuthUser currentUser = WebSecurityUtil.getCurrentUser();
        WelfareShop welfareShop = welfareshopMapper.selectById(id);
        EscortInfo escortInfo = escortInfoMapper.selectById(currentUser.getId());
        long l = escortInfo.getScore() - (count * welfareShop.getRedeemPrice());
        if (l < 0L) {
            throw new BusinessException("积分余额不足");
        }
        escortInfo.setScore(l);

        Integer num = welfareShop.getCount();
        int i = num - count;
        if (i < 0) {
            throw new BusinessException("库存不足");
        }
        welfareShop.setCount(i);
        escortInfoMapper.updateById(escortInfo);
        welfareshopMapper.updateById(welfareShop);
    }

    /**
     * 删除商品
     *
     * @param dto
     */
    @Override
    @Transactional
    public void deleteShop(IdDTO dto) {
        Asserts.notEmpty(dto.getIds(), "id不能为空");
        List<WelfareShop> welfareShops = welfareshopMapper.selectBatchIds(dto.getIds());
        welfareshopMapper.deleteByIds(welfareShops.stream().map(WelfareShop::getId).collect(Collectors.toList()));
    }
}
